package blog.controller;

import blog.entity.Page;
import blog.service.PageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller @RequestMapping("/page")
public class PageController {
    @Resource
    private PageService pageService;
    @RequestMapping("")
    public String gotoPageList(Map map){
        List<Page> pageList = pageService.getList();
        map.put("pageList",pageList);
        return "page/page-list";
    }
    @RequestMapping("/delete/{id}")
    public String deletePage(@PathVariable("id") Integer id){
        pageService.deletePageById(id);
        return "forward:/page";
    }
    @RequestMapping("/edit/{id}")
    public String gotoEdit(@PathVariable("id") Integer id,Map map){
        Page page = pageService.getPageById(id);
        map.put("page",page);
        return "page/page-editor";
    }
    @RequestMapping("/UpdatePage")
    public String updatePage(Page page){
        page.setPageUpdateTime(new Date());
        pageService.updatePage(page);
        return "forward:/page";
    }
    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    public String gotoInsert(){
        return "page/page-add";
    }
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public String addPage(Page page){
        page.setPageUpdateTime(new Date());
        page.setPageCreateTime(new Date());
        page.setPageCommentCount(0);
        page.setPageStatus(1);
        page.setPageViewCount(0);
        pageService.addPage(page);
        return "forward:/page";
    }
}
